package io.babystar.modules.business.service.impl;

import io.babystar.common.utils.DateUtils;
import io.babystar.modules.app.dto.StudentDayCourseDateDto;
import io.babystar.modules.business.dao.CourseDao;
import io.babystar.modules.business.dao.StudentCourseDateDao;
import io.babystar.modules.business.dao.StudentDao;
import io.babystar.modules.business.entity.CourseEntity;
import io.babystar.modules.business.entity.StudentCourseDateEntity;
import io.babystar.modules.business.entity.StudentEntity;
import io.babystar.modules.business.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.Query;

import io.babystar.modules.business.dao.CourseDateDao;
import io.babystar.modules.business.entity.CourseDateEntity;
import io.babystar.modules.business.service.CourseDateService;


@Service("courseDateService")
public class CourseDateServiceImpl extends ServiceImpl<CourseDateDao, CourseDateEntity> implements CourseDateService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private CourseDao courseDao;
    @Autowired
    private StudentCourseDateDao studentCourseDateDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseDateEntity> page = this.page(
                new Query<CourseDateEntity>().getPage(params),
                new QueryWrapper<CourseDateEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<StudentDayCourseDateDto> studentDayCourseDate(Map<String, Object> params) {
        return this.baseMapper.studentDayCourseDate(params);
    }

    @Override
    public List<List<StudentDayCourseDateDto>> schoolWeekTimetable(Map<String, Object> params) {
        List<List<StudentDayCourseDateDto>> lists = new ArrayList<>();
        List<String> appointmentDates = (List<String>)params.get("appointmentDates");
        List<String> courseWeeks = (List<String>)params.get("courseWeeks");
        List<String> courseWeekDates = (List<String>)params.get("courseWeekDates");
        //Map<String, Object> weekCurriculums = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("parentId", params.get("parentId"));
        param.put("shopId", params.get("shopId"));
        for(int i=0; i < appointmentDates.size(); i++){
            param.put("appointmentDate", appointmentDates.get(i));
            param.put("courseWeek", courseWeeks.get(i));
            param.put("courseDate", courseWeekDates.get(i));
            List<StudentDayCourseDateDto> studentDayCourseDates = this.baseMapper.studentDayCourseDate(param);
            lists.add(studentDayCourseDates);
        }
        return lists;
    }

    /*@Override
    public Map<String, Object> schoolWeekTimetable(Map<String, Object> params) {
        String startDate = params.get("startDate").toString();
        String endDate = params.get("endDate").toString();
        Date startDated = DateUtils.stringToDate(params.get("startDate").toString(), "yyyy-MM-dd");
        Map<String, Object> weekCurriculums = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        for(int i=0; i < 7; i++){
            if(i==0){
                param.put("courseDate", startDate);
                List<StudentDayCourseDateDto> studentDayCourseDates = this.baseMapper.studentDayCourseDate(param);
                weekCurriculums.put(startDate, studentDayCourseDates);
            }else{
                String date = DateUtils.format(DateUtils.addDateDays(startDated, i));
                param.put("courseDate", date);
                List<StudentDayCourseDateDto> studentDayCourseDates = this.baseMapper.studentDayCourseDate(param);
                weekCurriculums.put(date, studentDayCourseDates);
                if(date.equals(endDate)){//正常情况下循环一周，如有特殊情况，直接跳出
                    break;
                }
            }
        }
        return weekCurriculums;
    }*/

    @Override
    public PageUtils studentHasAppointment(Map<String, Object> params) {
        Query query = new Query();
        IPage<StudentDayCourseDateDto> page = query.getPage(params);
        List<StudentDayCourseDateDto> philiUserSubCardEntities = this.baseMapper.studentHasAppointment(params);
        page.setRecords(philiUserSubCardEntities);
        return new PageUtils(page);
    }

    @Override
    public List<CourseDateEntity> queryCourseList(Map<String, Object> params) {
        return this.baseMapper.queryCourseList(params);
    }

    /**
     *获取课程的所有上课时间
     * @param id
     * @return
     */
    @Override
    public List<StudentDayCourseDateDto> getCourseTimes(int id){
        Map<String,Object> cdp = new HashMap<String,Object>();
        cdp.put("id",id);
        return this.baseMapper.getCourseTimes(cdp);
    }

    @Override
    public List<StudentDayCourseDateDto> dayCourseDate(Map<String, Object> params){
        String courseDate = (String)params.get("courseDate");
        Integer parentId = (Integer) params.get("parentId");
        String courseWeek = (String) params.get("courseWeek");
        Map<String,Object> p = new HashMap<String,Object>();
        p.put("parent_id",parentId);
        List<StudentEntity> students = studentDao.selectByMap(p);
        // 查询某天所有的课程
        List<StudentDayCourseDateDto> list = this.baseMapper.dayCourseDate(params);
        for(StudentDayCourseDateDto dto : list){
            Map<String,Object> cdp = new HashMap<String,Object>();
            cdp.put("id",dto.getCourseId());
            cdp.put("courseWeek",courseWeek);
            List<StudentDayCourseDateDto> times = this.baseMapper.getCourseTimes(cdp);
            for(StudentDayCourseDateDto time: times){
                //校验课程是否被预约
                boolean flag = this.isAppointCourseDate(courseDate, time, students);
                if(!flag){
                    //不可约
                    time.setStatus(0);
                }
            }

            dto.setTimesList(times);
        }
        return list;
    }

    /**
     * 判断孩子否已经约过课程
     * @param time
     * @param students
     * @return true可约 false不可约
     */
    public boolean isAppointCourseDate(String courseDate, StudentDayCourseDateDto time, List<StudentEntity> students){
        Integer courseId = time.getCourseId();
        Long courseTimeId = time.getId();
        String courseTimeDesc = courseDate + " " + time.getCourseWeekZh();
        //false不可预约
        boolean flag = false;
        if(students.size()==0){
            flag = true;
        }
        for(StudentEntity student: students) {
            Integer studentId = student.getId();
            Map<String, Object> p = new HashMap<>();
            p.put("course_id", courseId);
            p.put("student_id", studentId);
            p.put("course_time_id", courseTimeId);
            p.put("course_time_desc", courseTimeDesc);
            List<StudentCourseDateEntity> list = studentCourseDateDao.selectByMap(p);
            if (list != null && list.size() > 0) {
                //相同的日期已经预约过
            }else{
                flag = true;
            }
        }
        return flag;
    }

    /**
     * 判断是否达到上线
     * @param courseDate
     * @param time
     * @return true可约 false不可约
     */
    public boolean isLimitNum(String courseDate, StudentDayCourseDateDto time){
        Integer courseId = time.getCourseId();
        Long courseTimeId = time.getId();
        String courseTimeDesc = courseDate + " " + time.getCourseWeekZh();
        //查询当前课程已经预约的人数
        Map<String, Object> p2 = new HashMap<>();
        p2.put("courseId", courseId);
        p2.put("courseTimeId", courseTimeId);
        p2.put("courseTimeDesc", courseTimeDesc);
        List<StudentCourseDateEntity> list2 = studentCourseDateDao.selectByMap(p2);
        //查询系统设置人数
        CourseEntity course = courseDao.selectById(courseId);
        Integer limitsNum = course.getLimitsNum();

        //false不可预约
        boolean flag = false;
        if (list2 != null && limitsNum != null && list2.size() >= limitsNum) {
            //return R.error("课程已经约满！");
            flag = false;
        }else{
            flag = true;
        }
        return flag;
    }

}
